package com.seewo.dubbo.debugger.sample.provider.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.seewo.dubbo.debugger.sample.api.User;
import com.seewo.dubbo.debugger.sample.api.UserService;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author chenliangliang
 * @date 2018/7/24
 */
@Service(retries = 2,timeout = 5000,version = "1.0")
public class UserServiceImpl implements UserService {

    private static ConcurrentHashMap<Integer,User> userMap=new ConcurrentHashMap<>();
    private static AtomicInteger count=new AtomicInteger(0);
    static {
        userMap.put(count.incrementAndGet(),new User(1,"json","男"));
        userMap.put(count.incrementAndGet(),new User(2,"Tom","男"));
        userMap.put(count.incrementAndGet(),new User(3,"jim","女"));
    }

    @Override
    public User getUserById(Integer id) {
        return userMap.get(id);
    }

    @Override
    public void addUser(User user) {
        user.setId(count.incrementAndGet());
        userMap.put(user.getId(),user);
    }

    @Override
    public void deleteById(Integer id) {
        userMap.remove(id);
    }

    @Override
    public User addUser(String sex, String name) {
        User user=new User(count.incrementAndGet(),name,sex);
        userMap.put(user.getId(),user);
        return user;
    }

    @Override
    public List<User> getAll() {
        return new ArrayList<>(userMap.values());
    }
}
